<?php

namespace app\admin\controller\wdsxh;

use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;

/**
 * 商会级别分类
 *
 * @icon fa fa-circle-o
 */
class MemberUnitLevel extends Backend
{
    
    /**
     * MemberUnitLevel模型对象
     * @var \app\admin\model\wdsxh\MemberUnitLevel
     */
    protected $model = null;
    protected $unitModel = null;

    protected $noNeedRight = ['selectpage'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\wdsxh\MemberUnitLevel;
        $this->unitModel = new \app\admin\model\wdsxh\MemberUnit;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("isCompanyList", $this->model->getIsCompanyList());
    }

    public function del($ids = null)
    {
        if (false === $this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ?: $this->request->post("ids");

        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $this->model->where($this->dataLimitField, 'in', $adminIds);
        }

        $unitCount = $this->unitModel->where('level_id',$ids)->count();
        if ($unitCount) {
            $this->error('请先删除企业列表');
        }

        $levelObj = $this->model->get($ids);
        $result = false;
        Db::startTrans();
        try {
            $result = $levelObj->delete();
            Db::commit();
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result) {
            $this->success();
        }
        $this->error(__('No rows were deleted'));
    }

    public function selectpage()
    {
        return parent::selectpage(); // TODO: Change the autogenerated stub
    }
}
